//
//  YLMonthlyBillCalendarViewModel.m
//  YLFinanceService
//
//  Created by she1323 on 2017/10/30.
//  Copyright © 2017年 she1323. All rights reserved.
//

#import "YLMonthlyBillCalendarViewModel.h"

#import "WHNetworkTool.h"

#import "YLAccountTool.h"

#import "YLMonthlyBillCalendarModel.h"

#import "WHProgressHUD.h"

#import "YLMonthlyBillCalendarListModel.h"

@implementation YLMonthlyBillCalendarViewModel

- (RACCommand *)requestCommand
{
    if (!_requestCommand) {
        __weak typeof(self) weakSelf = self;
        _requestCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
            
            return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
                
                NSString *url = [NSString stringWithFormat:@"%@AppCommon/PGetInvestmentChangeInfo", YLURL];
                
                [[WHNetworkTool shareInstance] sendPostWithUrl:url header:[YLAccountTool header] params:@{@"TimeIndex" : input} success:^(id responseObject) {
                    if ([responseObject[@"httpCode"] integerValue] == 200) {
                        
                        YLMonthlyBillCalendarItem *item = [YLMonthlyBillCalendarItem yy_modelWithDictionary:responseObject[@"httpResult"]];
                        
                        [weakSelf.dates addObjectsFromArray:item.item];
                        [subscriber sendNext:nil];

                    } else {
                        [WHProgressHUD showError:responseObject[@"message"]];
                    }
                    
                    [subscriber sendCompleted];
                    
                } fail:^(NSError *error) {
                    [subscriber sendCompleted];
                    
                }];

                
                return nil;
            }];
            
        }];
    }
    return _requestCommand;
}

- (RACCommand *)listCommand
{
    if (!_listCommand) {
        
        __weak typeof(self) weakSelf = self;
        _listCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
            
            return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
                
                NSString *url = [NSString stringWithFormat:@"%@AppCommon/PGetInvestmentChangeList", YLURL];
                
                NSMutableDictionary *params = input;
                NSInteger count = 10;
                params[@"MaxResultCount"] = @(count);
                
                NSInteger index = [params[@"PageIndex"] integerValue];
                
                
                [[WHNetworkTool shareInstance] sendPostWithUrl:url header:[YLAccountTool header] params:input success:^(id responseObject) {
                    if ([responseObject[@"httpCode"] integerValue] == 200) {
                        if (index == 1) {
                            [weakSelf.lists removeAllObjects];
                        }
                        
                        YLMonthlyBillCalendarListItem *listItem = [YLMonthlyBillCalendarListItem yy_modelWithDictionary:responseObject[@"httpResult"]];
                            [weakSelf.lists addObjectsFromArray:listItem.item];
                        
                        
                        [subscriber sendNext:nil];
                        
                        if (count > listItem.item.count) {
                            [weakSelf.wh_noMoreDataSubject sendNext:nil];
                        }
                        
                    } else {
                        [WHProgressHUD showError:responseObject[@"message"]];
                         [weakSelf.wh_loadDataErrorSubject sendNext:nil];
                    }
                    
                    [subscriber sendCompleted];
                } fail:^(NSError *error) {
                    [weakSelf.wh_loadDataErrorSubject sendNext:nil];
                    [subscriber sendCompleted];
                }];
                
                
                return nil;
            }];
            
        }];
    }
    return _listCommand;
}

- (NSMutableArray *)dates
{
    if (!_dates) {
        _dates = [NSMutableArray array];
    }
    return _dates;
}

- (NSMutableArray *)lists
{
    if (!_lists) {
        _lists = [NSMutableArray array];
    }
    return _lists;
}

- (void)dealloc
{
    [WHProgressHUD dismiss];
    [[WHNetworkTool shareInstance] cancel];
}

@end
